// pages/finance/basicList/operation/operation.js
import {
  formatTime
} from '../../../../utils/util'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    date:2016-9-12,
    array: [],
    index:0,
    customerList:[],
    // get_account:'',
    // get_bank:'',
    // remarks:'',
    shouldGet:0,
    alreadyGet:0,
    list:{
      get_bank:'',
      get_account:'',
      get_money:'',
      remarks:'',
    }

    },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let date=formatTime(new Date())
    this.setData({
      date
    })
    this.init(date);
  },
  init(){
    wx.request({
      url:'http://master.gddzxcx.com/'+'mobileadmin/receipt_money_table_add',
      data:{
        token:wx.getStorageSync('token'),
        start_date:this.data.date
      },
      method:'POST',
      success:res=>{
        console.log(res)
      let customerList=this.data.customerList;
      let alreadyGet=this.data.alreadyGet;
      let shouldGet=this.data.shouldGet;
      let index=this.data.index;
        if(res.data.code===200)
        res.data.data.collection.forEach(item=>customerList.push(item.customer_name))
        alreadyGet=res.data.data.collection[index].order_get.order_already_get;
        shouldGet=res.data.data.collection[index].order_get.order_should_get;
        this.setData({
          array:res.data.data,
          date:res.data.data.date,
          customerList,
          alreadyGet,
          shouldGet
        })
      },
      fail:err=>{
        console.log(err)
      }
    })
  },
  bindDateChange(e){
    this.setData({
      date: e.detail.value
    })
  },
  bindPickerChange(e){
    let array=this.data.array;
    let index=this.data.index;
    let list=this.data.list;
    index=e.detail.value;
    list.get_money='';
    list.get_account='';
    list.get_bank='';
    list.remarks='';
    console.log(list)
    this.setData({
      index,
      shouldGet:array.collection[index].order_get.order_should_get,
      alreadyGet:array.collection[index].order_get.order_already_get,
      list,
    })
  },
  onChange(e){
    let list=this.data.list;
    let values=e.currentTarget.dataset.value;
    list[values]=e.detail;
    this.setData({
      list,
    })
  },
  judge(){
    let list=this.data.list;
    let shouldGet=this.data.shouldGet;
    let alreadyGet=this.data.alreadyGet;
    let money=shouldGet - alreadyGet;
    for(var key in list){
      console.log(list[key])
      if(list[key]==''&&key!='remarks'){
        wx.showToast({
          title: '请填写完整的信息，再提交',
          icon:'none',
          duration:1500
        })
        break;
      }else{
        if(+ list.get_money>money||+list.get_money===0){
          wx.showToast({
            title: '所输入的金额不符',
            icon:'none',
            duration:1500
          })
        }else{
          return true;
        }
      }
    }
  },
  base(){
    let index=this.data.index;
    let array=this.data.array;
    if(this.judge()){
     wx.request({
      url:'http://master.gddzxcx.com/'+'mobileadmin/receipt_money_table_add_do',
      data:{
        token:wx.getStorageSync('token'),
        start_date:this.data.date,
        customer_id:array.collection[index].id,
        get_bank:this.data.list.get_bank,
        get_account:this.data.list.get_account,
        get_money:this.data.list.get_money,
        remarks:this.data.list.remarks
      },
      success:res=>{
        console.log(res)
        if(res.data.code===200){
          wx.showToast({
            title: res.data.message,
            icon:'none',
            duration:1500
          })
        }else{
          wx.showToast({
            title: res.data.message,
            icon:'none',
            duration:3000
          })
        }
      },
      fail:err=>{
        console.log(err)
      }
    })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },


})